package com.hougong.lineclient.utils;

import com.hougong.lineclient.BodyToBytes;
import com.hougong.lineclient.transport.BaseFrame;
import com.hougong.lineclient.transport.ClientConfig;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
;

/**
 * 发送消息到通道channel中
 */
public class SendUtils {
    private static final String TAG= "发送工具sendutils";
    private static final Logger LOGGER=LoggerFactory.getLogger(TAG);
    private SendUtils(){}
    public static void sendResponse(Channel channel, short bizType, BodyToBytes response){
        BaseFrame baseFrame=new BaseFrame();
        baseFrame.setMessageType((byte) 0x02);
        baseFrame.setBizType(bizType);
        byte[] body=response.toBytes();
        baseFrame.setBody(body);
        ChannelFuture channelFuture = channel.writeAndFlush(baseFrame);
        channelFuture.addListener(new ChannelFutureListener() {
            @Override
            public void operationComplete(ChannelFuture future) throws Exception {
                if (future.isSuccess()){
//                    Log.i(TAG,"业务类型:0x"+StringUtils.bytesToHexString(NumberUtils.shortToByte(bizType))+" body:"+StringUtils.bytesToHexString(body)+ " Response消息发送成功");
                }else {
                    channel.writeAndFlush(baseFrame)
                            .addListener(new ChannelFutureListener() {
                                @Override
                                public void operationComplete(ChannelFuture future) throws Exception {
                                    if(future.isSuccess()){
//                                        Log.i(TAG,"业务类型:0x"+StringUtils.bytesToHexString(NumberUtils.shortToByte(bizType))+" body:"+StringUtils.bytesToHexString(body)+ " Response消息发送成功");
                                    }else {
                                        LOGGER.error("业务类型:0x"+StringUtils.bytesToHexString(NumberUtils.shortToByte(bizType))+" body:"+StringUtils.bytesToHexString(body)+ " Response消息发送失败");
                                    }
                                }
                            });
                }
            }
        });
    }
    public static void sendNotice(Channel channel, short bizType, BodyToBytes response){
        BaseFrame baseFrame=new BaseFrame();
        baseFrame.setMessageType((byte) 0x03);
        baseFrame.setBizType(bizType);
        byte[] body=response.toBytes();
        baseFrame.setBody(body);
        ChannelFuture channelFuture = channel.writeAndFlush(baseFrame);
        channelFuture.addListener(new ChannelFutureListener() {
            @Override
            public void operationComplete(ChannelFuture future) throws Exception {
                if (future.isSuccess()){
//                    Log.i(TAG,"业务类型:0x"+bizType+" body:"+StringUtils.bytesToHexString(body)+ "Notice消息发送成功");
                }else {
                    channel.writeAndFlush(baseFrame)
                            .addListener(new ChannelFutureListener() {
                                @Override
                                public void operationComplete(ChannelFuture future) throws Exception {
                                    if(future.isSuccess()){
//                                        Log.i(TAG,"业务类型:0x"+StringUtils.bytesToHexString(NumberUtils.shortToByte(bizType))+" body:"+StringUtils.bytesToHexString(body)+ " Notice消息发送成功");
                                    }else {
                                        LOGGER.error("业务类型:0x"+StringUtils.bytesToHexString(NumberUtils.shortToByte(bizType))+" body:"+StringUtils.bytesToHexString(body)+ " Notice消息发送失败");
                                    }
                                }
                            });
                }
            }
        });
    }

    /**
     * 发送消息到连接通道
     * @param bizType
     * @param request
     */
    public static void sendRequest(short bizType,BodyToBytes request){
        if(ClientConfig.channel!=null) {
            BaseFrame baseFrame = new BaseFrame();
            baseFrame.setMessageType((byte) 0x00);
            baseFrame.setBizType(bizType);
            byte[] body = new byte[0];
            if (request != null) {
                body = request.toBytes();
            }
            baseFrame.setBody(body);
            ChannelFuture channelFuture = ClientConfig.channel.writeAndFlush(baseFrame);

            byte[] finalBody = body;
            channelFuture.addListener(new ChannelFutureListener() {
                @Override
                public void operationComplete(ChannelFuture future) throws Exception {
                    if (future.isSuccess()) {
//                        Log.i(TAG, "业务类型:0x" + bizType + " body:" + StringUtils.bytesToHexString(finalBody) + "Request消息发送成功");
                    } else {
                        ClientConfig.channel.writeAndFlush(baseFrame)
                                .addListener(new ChannelFutureListener() {
                                    @Override
                                    public void operationComplete(ChannelFuture future) throws Exception {
                                        if (future.isSuccess()) {
//                                            Log.i(TAG, "业务类型:0x" + StringUtils.bytesToHexString(NumberUtils.shortToByte(bizType)) + " body:" + StringUtils.bytesToHexString(finalBody) + " Request消息发送成功");
                                        } else {
                                            LOGGER.error(TAG, "业务类型:0x" + StringUtils.bytesToHexString(NumberUtils.shortToByte(bizType)) + " body:" + StringUtils.bytesToHexString(finalBody) + " Request消息发送失败");
                                        }
                                    }
                                });
                    }
                }
            });
        }else {
            LOGGER.error(TAG,"通道已经关闭，无法发送心跳");
        }
    }

    public static ChannelFuture sendRequestWithFuture(short bizType, BodyToBytes request){
        if(ClientConfig.channel!=null) {
            BaseFrame baseFrame = new BaseFrame();
            baseFrame.setMessageType((byte) 0x00);
            baseFrame.setBizType(bizType);
            byte[] body = null;
            if (request != null) {
                body = request.toBytes();
                baseFrame.setBody(request.toBytes());
            }
            ChannelFuture channelFuture = ClientConfig.channel.writeAndFlush(baseFrame);

            return channelFuture;
        }else {
            return null;
        }
    }
}
